<template>
  <div>
    <el-dialog :visible.sync="$parent.open" title="完成盘点" width="780px">
      <div class="div_dialog">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
          <el-form-item label="盘点结果" prop="status">
            <el-select v-model="formData.status" placeholder="请选择盘点结果" :style="{ width: '100%' }">
              <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.label" :value="item.value"
                :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账上库存数量" prop="amount">
            <el-input-number v-model="formData.amount" disabled controls-position="right" :style="{ width: '100%' }"
              :min="1">
            </el-input-number>
          </el-form-item>
          <el-form-item label="实际库存数量" prop="actualAmount">
            <el-input-number v-model="formData.actualAmount" controls-position="right" :style="{ width: '100%' }"
              :min="0">
            </el-input-number>
          </el-form-item>
          <el-form-item label="盘点时间" prop="checkTime">
            <el-date-picker v-model="formData.checkTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
              :style="{ width: '100%' }" placeholder="请选择盘点时间"></el-date-picker>
          </el-form-item>
          <el-form-item label="说明" prop="remark">
            <el-input v-model="formData.remark" type="textarea" placeholder="请输入说明"
              :autosize="{ minRows: 4, maxRows: 4 }" :style="{ width: '100%' }"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" align="center">
        <el-button @click="() => $parent.open = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// import { getplacelist } from '@/api/equipment/equipment.js';
// import { putMstktakingTaskGenEquipment } from "@/api/Inventory/Inventory.js";
import { putConsumable } from "@/api/deviceConsumable/inventoryCheck/index.js";
export default {
  props: ['rowArr'],
  components: {},
  data () {
    return {
      formData: {
        status: undefined,
        amount: undefined,
        actualAmount: undefined,
        checkTime: null,
        remark: undefined,
      },
      rules: {
        status: [{
          required: true,
          message: '请选择盘点结果',
          trigger: 'change'
        }],
        amount: [{
          required: true,
          message: '请输入账上库存数量',
          trigger: 'change'
        }],
        actualAmount: [{
          required: true,
          validator: (rule, value, callback) => {
            if (this.formData.status === '0') {
              callback();
            }
            if (value === '') {
              callback(new Error('请输入实际库存数量'));
            } else {
              switch (this.formData.status) {
                case '0':
                  callback();
                  break;
                case '1':
                  if (this.formData.amount * 1 !== value * 1) {
                    callback(new Error('已盘点需账上库存与实际库存相同'));
                  } else {
                    callback();
                  }
                  break;
                case '2':
                  if (this.formData.amount * 1 <= value * 1) {
                    callback(new Error('盘亏需实际库存小于账上库存'));
                  } else {
                    callback();
                  }
                  break;
                case '3':
                  if (this.formData.amount * 1 >= value * 1) {
                    callback(new Error('盘盈需实际库存大于账上库存'));
                  } else {
                    callback();
                  }
                  break;
              }
            }
          },
          trigger: 'blur'
        }],
        checkTime: [{
          required: true,
          message: '请选择盘点时间',
          trigger: 'change'
        }],
        remark: [],
      },
      statusOptions: [{
        "label": "未盘点",
        "value": '0'
      }, {
        "label": "已盘点",
        "value": '1'
      }, {
        "label": "盘亏",
        "value": '2'
      }, {
        "label": "盘盈",
        "value": '3'
      }],
      newPlaceIdOptions: [],
    }
  },
  computed: {
    name () {
      return this.$store.state.user.nickName
    }
  },
  watch: {},
  created () {
    this.formData.amount = this.rowArr[0].amount
  },
  mounted () { },
  methods: {
    handleConfirm () {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        console.log(this.rowArr);
        let params = this.rowArr.map(item => {
          return {
            ...this.formData,
            checkStockId: this.rowArr[0].checkStockId,
            consumableBaseId: item.consumableBaseId
          }
        })
        putConsumable(params).then(res => {
          if (res.code === 200) {
            this.$message.success('操作成功')
            this.$parent.open = false
            this.$parent.getList()
          }

        }).catch(err => {
          console.log(err);
        })
      })
    },
  }
}

</script>
<style lang="scss" scoped>
.div_dialog {
  width: 60%;
  margin: 0 auto;
}
</style>
